package com.ctg.behavior.calc.impala.builder.usergroups;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.stereotype.Component;

import com.ctg.behavior.common.consts.CtFieldConsts;
import com.ctg.behavior.common.usergroups.CtUGCalcBody;
import com.ctg.behavior.common.usergroups.CtUGCalcData;
import com.ctg.behavior.common.usergroups.CtUGCalcResult;
import com.ctg.behavior.common.usergroups.CtUGPartitionValue;
import com.ctg.behavior.utils.CtDateUtils;

@Component
public class CtUGCalcResultBuilder {

	public CtUGCalcResult doBuilder(CtUGCalcResult calcResult, List<LinkedHashMap<String, Object>> queryResults, CtUGCalcBody ugCalcBody) {

		CtUGCalcData ugCalcData = new CtUGCalcData();
		calcResult.getValue().getList().add(ugCalcData);

		CtUGPartitionValue partitionValue = new CtUGPartitionValue();
		if(queryResults.isEmpty() || queryResults.get(0).isEmpty()) {
			partitionValue.setUserCount(0);
			calcResult.getValue().setTotal(0);
		} else {
			String textTotal = queryResults.get(0).get(CtFieldConsts.FIELD_KEY_TOTAL).toString();
			partitionValue.setUserCount(Integer.valueOf(textTotal));
			calcResult.getValue().setTotal(1);
		}
		calcResult.getValue().setAppId(ugCalcBody.getAppId());
		calcResult.getValue().setBookmarkId(ugCalcBody.getBookmarkId());
		calcResult.getValue().setRequestId(ugCalcBody.getRequestId());
		calcResult.getValue().setDataUpdateTime(CtDateUtils.dateToString(new Date()));
		calcResult.getValue().setReportUpdateTime(CtDateUtils.dateToString(new Date()));
		
		Date dateOfZeroTime = CtDateUtils.formatDayStartTime(new Date());
		partitionValue.setBaseTime(CtDateUtils.dateToString(dateOfZeroTime));
		partitionValue.setFinishedTime(CtDateUtils.dateToString(new Date()));

		ugCalcData.getPartitionValues().add(partitionValue);
		
		return calcResult;
		
	}
	
}
